<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<link th:href="@{/ajax/libs/iCheck/custom.css}" rel="stylesheet"/>
<link href="/ajax/libs/jquery-ztree/3.5/css/metro/zTreeStyle.css"
      th:href="@{/ajax/libs/jquery-ztree/3.5/css/metro/zTreeStyle.css}" rel="stylesheet"/>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-role-add">


        <div class="form-group">
            <label class="col-sm-3 control-label ">标题：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="title" id="title"/>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">开始时间：</label>
            <div class="col-sm-8">
                <input readonly="readonly" name="startTime" id="startTime" class="form-control" type="text" placeholder="yyyy-MM-dd HH:mm:ss">
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">结束时间：</label>
            <div class="col-sm-8">
                <input readonly="readonly" name="endTime" id="endTime" class="form-control" type="text" placeholder="yyyy-MM-dd HH:mm:ss">
            </div>
        </div>


        <div class="form-group">
            <label class="col-sm-3 control-label">说明：</label>
            <div class="col-sm-8">
                <textarea rows="7" name="content" type="text" id="descr"
                          class="form-control"></textarea>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">是否共享：</label>
            <div class="col-sm-8">
                <div id="userTrees" class="ztree"></div>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">状态：</label>
            <div class="col-sm-8">
                <label class="checkbox-inline i-checks">
                   一般： <input name="status" type="radio" th:checked="true" value="0">
                </label>
                <label class="checkbox-inline i-checks">
                    重要：<input name="status" type="radio" value="1">
                </label>
                <label class="checkbox-inline i-checks">
                    紧急：<input name="status" type="radio" value="2">
                </label>
            </div>
        </div>



        <div class="form-group">
            <div class="form-control-static col-sm-offset-9">
                <button type="submit" class="btn btn-primary">提交</button>
                <button onclick="$.modal.close()" class="btn btn-danger" type="button">关闭</button>
            </div>
        </div>
    </form>
</div>
<div th:include="include::footer"></div>
<script th:src="@{/ajax/libs/jquery-ztree/3.5/js/jquery.ztree.all-3.5.js}"></script>
<script th:src="@{/ajax/libs/select/select2.js}"></script>
<script type="text/javascript">

    layui.use('laydate', function () {
        var laydate = layui.laydate;
        //时间选择器
        laydate.render({
            elem: '#startTime'
            , type: 'datetime'
        });
        laydate.render({
            elem: '#endTime'
            , type: 'datetime'
        });

    })

    // 树结构初始化加载
    $(function () {
        btn.treeUser();
    })


    $("#form-role-add").validate({
        rules: {
            title: {required: true,maxlength:10}
            , descr: {
                required: true,
                maxlength:150,
            }
            ,startTime:{
                required: true,
            }
            ,endTime:{
                required: true,
            }
        },
        submitHandler: function (form) {
            btn.save(btn.onCheck());
        }
    });

    var btn = {
        treeUser: function () {
            var zTreeObj,
                setting = {
                    view: {
                        selectedMulti: false
                    },
                    callback: {
                        beforeCheck: true,
                        onCheck: btn.onCheck
                    },
                    check: {
                        enable: true,
                        chkStyle: "checkbox",    //复选框
                        chkboxType: {"Y": "ps", "N": "ps"}
                    }

                },
                zTreeNodes = btn.initDeptData();
            zTreeObj = $.fn.zTree.init($("#userTrees"), setting, zTreeNodes);
        },
        onCheck: function (e, treeId, treeNode) {
            var menuIds = [];
            var treeObj = $.fn.zTree.getZTreeObj("userTrees"),
                nodes = treeObj.getCheckedNodes(true),
                v = "";
            for (var i = 0; i < nodes.length; i++) {
                if (nodes[i].id != null) {
                    menuIds.push(nodes[i].id);
                }

            }
            return menuIds;

        },
        //请求服务端获取数据
        initDeptData: function () {
            var array;
            $.ajaxSettings.async = false;
            $.post("/dept/getDeptAndUserTreeData", {}, function (data) {
                array = data;
            })
            return array;
        },
        save:function (menuIds) {
            $.ajax({
                cache: true,
                type: "POST",
                url: "/schedule/addSave",
                data: {
                    title:$("#title").val(),
                    startTime:$("#startTime").val(),
                    endTime:$("#endTime").val(),
                    descr:$("#descr").val(),
                    status:$.form.selectRadioCheckeds('status'),
                    userIds:menuIds
                },
                async: false,
                traditional: true,
                error: function (request) {
                    $.modal.alertError("系统错误");
                },
                success: function (data) {
                    $.operate.saveSuccess(data);
                }
            });
        }
    }
</script>
</body>
</html>
